package net.sunbeta.test.swing;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class UserLoginPanel extends JPanel {
    private static final int LEFT        = 0;
    private JTextField       userField   = new JTextField(10);
    private JLabel           errorLabel  = new JLabel();
    private JButton          loginButton = new JButton("Login");
    private JPasswordField   passField   = new JPasswordField(10);

    private class ConvertActionListener implements ActionListener {
        public void actionPerformed(ActionEvent event) {
            if (event.getSource() == loginButton) {
                if (userField.getText().trim().length() == 0) {
                    errorLabel.setText("Missing username");
                } else {
                    // do login stuff
                }
            }
        }
    }

    public UserLoginPanel() {
        addComponents();
        setLayout(new GridLayout(3, 2));
    }

    private void addComponents() {
        JLabel userLabel = new JLabel("Username", LEFT);
        userLabel.setName("user_label");
        add(userLabel);
        userField.setName("user_input");
        add(userField);
        JLabel passLabel = new JLabel("Password", LEFT);
        add(passLabel);
        passField.setName("pass_input");
        add(passField);
        errorLabel.setName("error_label");
        add(errorLabel);
        loginButton.addActionListener(new ConvertActionListener());
        add(loginButton);
    }
}
